package com.example.java05redis.controller;

import com.example.java05redis.entity.Person;
import com.example.java05redis.service.IPersonService;
import com.example.java05redis.service.impl.PersonServiceImpl;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PersonController {

    @Autowired
    private IPersonService personService;

    @Autowired
    private RBloomFilter<String> bloomFilter;

    @GetMapping("/person/{id}")
    public ResponseEntity<Person> getPersonById(@PathVariable Long id){
        return ResponseEntity.ok(personService.getPersonById(id));
    }

    @GetMapping("/init-person-filter")
    public ResponseEntity<List<Person>> initPersonFilter(){
        //查询所有人的id添加到布隆过滤器
        List<Person> list = personService.list(null);
        list.forEach(person -> {bloomFilter.add(PersonServiceImpl.PREFIX + person.getId());});
        return ResponseEntity.ok(list);
    }

    @PostMapping("/person")
    public ResponseEntity<String> addPerson(@RequestBody Person person){
        personService.save(person);
        return ResponseEntity.ok("ok");
    }

    @PutMapping("/person")
    public ResponseEntity<String> updatePerson(@RequestBody Person person){
        personService.updateById(person);
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("/person/{id}")
    public ResponseEntity<String> deletePerson(@PathVariable Long id){
        personService.removeById(id);
        return ResponseEntity.ok("ok");
    }
}
